import { getUserInfo } from "@/api/system/user";

const state = () => ({
  user: {},
});

const getters = {
  getMenus: (state) => {
    return state.user.menus;
  },
  getPermissions: (state) => {
    return state.user.permissions;
  },
  getUserInfo: (state) => {
    return state.user.userInfo;
  },
};

const actions = {
  getUser: ({ commit }) => {
    return new Promise((resolve, reject) => {
      getUserInfo()
        .then((response) => {
          const { data } = response;
          commit("setUser", data);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

const mutations = {
  setUser: (state, user) => {
    state.user = user;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
